package com.heimo.babyage.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

public class ExternalStorageFileHelper {
	private final String TAG = "ExternalStorageFileHelper";
	private Context ctx;

	public ExternalStorageFileHelper(Context ctx) {
		this.ctx = ctx;
	}

	public static boolean checkSDCard() {
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
		    // We can read and write the media
		    mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
		    // We can only read the media
		    mExternalStorageAvailable = true;
		    mExternalStorageWriteable = false;
		} else {
		    // Something else is wrong. It may be one of many other states, but all we need
		    //  to know is we can neither read nor write
		    mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		return (mExternalStorageAvailable&&mExternalStorageWriteable);
	}

	public void saveBitmap(String fileName, Bitmap bm) {
		// File file = new File(ctx.getExternalFilesDir(null), fileName);
		File file = new File(ctx.getFilesDir(), fileName);
		try {
			// FileOutputStream out = new FileOutputStream(file);
			// FileOutputStream out = ctx.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
			// FileOutputStream out = ctx.openFileOutput(fileName, Context.MODE_WORLD_WRITEABLE);

			File outFile = new File(ctx.getFilesDir(), fileName);
			//OutputStream out = new FileOutputStream(outFile);
			FileOutputStream out = ctx.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
			
			bm.compress(Bitmap.CompressFormat.PNG, 90, out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		LogHelper.log(TAG, "FileName:" + file);
	}

	public void saveFile(String fileName, File inFile) {
		// Create a path where we will place our private file on external
		// storage.
		// File file = new File(ctx.getExternalFilesDir(null), fileName);
		File outFile = new File(ctx.getFilesDir(), fileName);
		try {
			// Very simple code to copy a picture from the application's
			// resource into the external file. Note that this code does
			// no error checking, and assumes the picture is small (does not
			// try to copy it in chunks). Note that if external storage is
			// not currently mounted this will silently fail.
			InputStream is = new FileInputStream(inFile);
			OutputStream os = new FileOutputStream(outFile);
			byte[] data = new byte[is.available()];
			is.read(data);
			os.write(data);
			is.close();
			os.close();
		} catch (IOException e) {
			LogHelper.log(TAG, "Error writing " + outFile);
		}
	}

	public void delFile(String fileName) {
		// Get path for the file on external storage. If external
		// storage is not currently mounted this will fail.
		// File file = new File(ctx.getExternalFilesDir(null), fileName);
		File file = new File(ctx.getFilesDir(), fileName);
		if (file != null) {
			file.delete();
		}
	}

	public boolean hasFile(String fileName) {
		// Get path for the file on external storage. If external
		// storage is not currently mounted this will fail.
		// File file = new File(ctx.getExternalFilesDir(null), fileName);
		File file = new File(ctx.getFilesDir(), fileName);
		if (file != null) {
			return file.exists();
		}
		return false;
	}
}